package com.idconfirmsystem.remote;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpParams;

import android.util.Log;

import com.idconfirmsystem.Conf;
import com.idconfirmsystem.model.BaseModel;
import com.idconfirmsystem.model.NoticeQueryObj;
import com.idconfirmsystem.model.QueryObj;
import com.koushikdutta.async.future.FutureCallback;

/**
 * 与远程服务端交互类
 * 
 * @author wjd
 * 
 */
public class ConfirmApi {
	private static final String TAG = "ConfirmApi";

	/**
	 * 用户登录 2015-8-2
	 * 
	 * @param loginName
	 * @param password
	 * @param callBack
	 */
	public static void login(String loginName, String password, HttpCallBack callBack) {
	}

	public static void getTask(int taskType, FutureCallback futureCallback) {

	}

	/**
	 * 查询用户的消息 2015-8-30
	 * 
	 * @param queryObj
	 * @param callBack
	 */
	public static void queryMsg(NoticeQueryObj queryObj, HttpCallBack callBack) {
		SoapHelper soapHelper = new SoapHelper();
		String serviceName = "NoticeService";
		String met1hodName = "getNoticeList";
		Map<String, String> mProperties = new HashMap<String, String>();
		mProperties.put("type", queryObj.getType());
		mProperties.put("date", queryObj.getDate());
		soapHelper.get(serviceName, met1hodName, mProperties, callBack);
	}

	/**
	 * 通用查询方法 2015-8-30
	 * 
	 * @param q
	 *            查询对象
	 * @param serviceMethod
	 *            结构： LoginCheckService/loginCheck
	 * @param callBack
	 *            回调对象
	 */
	public static void queryData(QueryObj q, String serviceMethod, HttpCallBack callBack) {
		KJHttp kjp = new KJHttp();
		HttpParams kParams = new HttpParams();
		Class<?> c = q.getClass();
		Field[] f = c.getDeclaredFields();
		String key = "";
		Object value = "";
		for (int i = 0; i < f.length; i++) {
			key = f[i].getName();
			f[i].setAccessible(true);
			try {
				value = f[i].get(q);
				if (value != null) {
					kParams.put(key, value.toString());
					Log.d(TAG, "param:" + key + " ,value:" + value.toString());
				}
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String url = Conf.ServiceConf.getServiceUrl() + serviceMethod;
		Log.d("confirmapi", url);
		kjp.post(url, kParams, callBack);
	}

	public static void pushData(BaseModel vo, String serviceMethod, HttpCallBack callBack) {
		KJHttp kjp = new KJHttp();
		HttpParams kParams = new HttpParams();
		Class<?> c = vo.getClass();
		Field[] f = c.getDeclaredFields();
		String key = "";
		Object value = "";
		for (int i = 0; i < f.length; i++) {
			key = f[i].getName();
			f[i].setAccessible(true);
			try {
				value = f[i].get(vo);
				if (value != null) {
					kParams.put(key, value.toString());
					Log.d(TAG, "param:" + key + " ,value:" + value.toString());
				}
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String url = Conf.ServiceConf.getServiceUrl() + serviceMethod;
		Log.d("confirmapi", url);
		kjp.post(url, kParams, callBack);
	}
}
